package Tools;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import Tools.TModel.TCommand;
import Tools.TModel.TKey;

public class TController {

    private static List<String> parsedInputData;
    private static TModel model = new TModel();
    private static ACommand command;

    /**
     * Implement function of Controller of architecture MVC
     * @param args
     */
    public static void main(String[] args) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        boolean ok = false;
        List<String> validParameters = new ArrayList<String>(0);

        while (true) {
            System.out.print(model.getWorkDirectory() + ">");
            try {

                parsedInputData = model.parsingInputData(reader.readLine());
                command = model.initializeCommand(parsedInputData);
                validParameters = command.setParameters(parsedInputData);

                if (command.typeOfCommand == TCommand.copy) {
                    TKey key = null;
                    ok = false;
                    while (!ok) {
                        ok = command.action(validParameters, key);
                        if (!ok) {
                            System.out.print(command.getPrintResult());
                            String answer = reader.readLine().intern();
                            if (answer == "all") {
                                key = TKey.REPLACE_All;
                            }
                            if (answer == "no") {
                                key = TKey.REPLACE_NO;
                            }
                            if (answer == "yes") {
                                key = TKey.REPLACE_YES;
                            }
                        }
                    }
                    System.out.print(command.getPrintResult());
                } else if (command.typeOfCommand == TCommand.del) {
                    TKey key = null;
                    ok = false;
                    while (!ok) {
                        ok = command.action(validParameters, key);
                        if (!ok) {
                            System.out.print(command.getPrintResult());
                            String answer = reader.readLine().intern();

                            if (answer == "no") {
                                key = TKey.DELETE_NO;
                            }
                            if (answer == "yes") {
                                key = TKey.DELETE_YES;
                            }
                        }
                    }
                    System.out.print(command.getPrintResult());
                } else {
                    ok = command.action(validParameters, null);

                    if (ok) {
                        System.out.print(command.getPrintResult());
                    }
                }

            } catch (Exception e) {

                System.out.print(e.toString() + "\n");
            }
        }
    }
}
